import 'package:flutter/material.dart';
import 'package:flutter_duqin/root_pages/home_page.dart';
import 'package:flutter_duqin/root_pages/music_page.dart';
import 'package:flutter_duqin/root_pages/profile_page.dart';
import 'package:flutter_duqin/root_pages/tiny_video_page.dart';
import 'package:proste_indexed_stack/proste_indexed_stack.dart';

import 'config/app_colors.dart';

class RootPage extends StatefulWidget {
  const RootPage({Key? key}) : super(key: key);

  @override
  State<RootPage> createState() => _RootPageState();
}

const Map<String, String> _bottomNames = {
  'home': '首页',
  'music': '音乐',
  'create_media': '',
  'tiny_video': '小视频',
  'profile': '我的'
};

class _RootPageState extends State<RootPage> {
  // 当前选中也索引
  int _currentIndex = 0;
  // 底部导航数组
  final List<BottomNavigationBarItem> _bottomNavBarList = [];
  // 页面集合
  final List<IndexedStackChild> _pages = [
    IndexedStackChild(child: const HomePage()),
    IndexedStackChild(child: const MusicPage()),
    IndexedStackChild(child: Container()),
    IndexedStackChild(child: const TinyVideoPage()),
    IndexedStackChild(child: const ProfilePage())
  ];

  void _onTabClick(int index) {
    if (index == 2) {
      return _onCreateMedia();
    }

    setState(() {
      _currentIndex = index;
    });
  }

  void _onCreateMedia() {
    print('_onCreateMedia');
  }

  @override
  void initState() {
    super.initState();

    _bottomNames.forEach((key, value) {
      _bottomNavBarList.add(_bottomNavBarItem(key, value));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // body: _pages[_currentIndex],
      // body: IndexedStack(
      //   index: _currentIndex,
      //   children: _pages,
      // ),
      body: ProsteIndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: BottomNavigationBar(
          items: _bottomNavBarList,
          currentIndex: _currentIndex,
          onTap: _onTabClick,
          type: BottomNavigationBarType.fixed),
      floatingActionButton: _createMediaButton(),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }

  Widget _createMediaButton() {
    return Container(
      width: 44,
      height: 44,
      // color: ,
      margin: const EdgeInsets.only(top: 56),
      child: FloatingActionButton(
        backgroundColor: AppColors.nav,
        child: Image.asset(
          'assets/images/icons/create_media.png',
        ),
        onPressed: _onCreateMedia,
      ),
    );
  }

  BottomNavigationBarItem _bottomNavBarItem(String key, String value) {
    return BottomNavigationBarItem(
        icon: Image.asset(
          'assets/images/icons/$key.png',
          width: 24,
          height: 24,
        ),
        activeIcon: Image.asset(
          'assets/images/icons/${key}_active.png',
          width: 24,
          height: 24,
        ),
        label: value,
        tooltip: '');
  }
}
